package designs.models.composite.example.one;

import java.util.Iterator;

public class Computer {
	public static double computerWeight(TreeComponent node) {
		double weightSum = 0;
		if (node.isLeaf() == true) {
			weightSum += node.getWeight();
		}
		
		if (node.isLeaf() == false) {
			weightSum += node.getWeight();
			Iterator<TreeComponent> allChild = node.getAllChild();
			while (allChild.hasNext()) {
				TreeComponent treeComponent = (TreeComponent) allChild.next();
				weightSum+=computerWeight(treeComponent);
			}
		}
		return weightSum;
	}
	
	public  static double computerValue(TreeComponent node,double unit) {
		double appleWorth = 0;
		if (node.isLeaf() == true) {
			appleWorth = appleWorth + node.getWeight()*unit;
		}
		
		if (node.isLeaf() == false) {
			appleWorth = appleWorth + node.getWeight()*unit;
			Iterator<TreeComponent> allChild = node.getAllChild();
			while (allChild.hasNext()) {
				TreeComponent treeComponent = (TreeComponent) allChild.next();
				appleWorth = appleWorth +computerValue(treeComponent,unit);
			}
		}
		return appleWorth ;
	}
	
	public static String getAllChildrenName(TreeComponent node) {
		
		StringBuffer mess = new StringBuffer();
		if (node.isLeaf() == true) {
			mess.append(" " + node.toString());
		}
		
		if (node.isLeaf() == false) {
			mess.append(" "+node.toString());
			Iterator<TreeComponent> allChild = node.getAllChild();
			while (allChild.hasNext()) {
				TreeComponent treeComponent = (TreeComponent) allChild.next();
				mess.append(getAllChildrenName(treeComponent));
			}
		}
		return  new String(mess);
	}
}
